
#ifndef APPLE_APP_STUB_H
#define APPLE_APP_STUB_H

#ifdef  __cplusplus
extern "C" {
#endif

#include <stdio.h>
#include <stdint.h>

typedef signed char             int8;
typedef signed short            int16;
typedef signed long             int32;
typedef signed long long        int64;
typedef unsigned char           uint8;
typedef unsigned short          uint16;
typedef unsigned long           uint32;
typedef unsigned long long      uint64;
typedef int            ipod_t;

typedef int32_t		StubAirPlayEntity;
#define kStubAirPlayEntity_NotApplicable			0
#define kStubAirPlayEntity_Controller				1
#define kStubAirPlayEntity_Accessory				2

// AirPlaySpeechMode

typedef int32_t		StubAirPlaySpeechMode;
#define kStubAirPlaySpeechMode_NotApplicable			0
#define kStubAirPlaySpeechMode_None						-1 // No speech-related states are active.
#define kStubAirPlaySpeechMode_Speaking					1  // Device is speaking to the user.
#define kStubAirPlaySpeechMode_Recognizing				2  // Device is recording audio to recognize speech from the user.


// State

typedef struct
{
	StubAirPlayEntity			entity;
	StubAirPlaySpeechMode		mode;

}	StubAirPlaySpeechState;

typedef struct
{
	StubAirPlayEntity			screen;			// Current user of the screen.
	StubAirPlayEntity			permScreen;		// Permanent owner of screen.
	StubAirPlayEntity			mainAudio;		// Current user of main audio.
	StubAirPlayEntity			permMainAudio;	// Permanent owner of main audio.
	StubAirPlayEntity			phoneCall;		// Owner of phone call.
	StubAirPlaySpeechState		speech;			// Owner of speech and its mode.
	StubAirPlayEntity			turnByTurn;		// Owner of navigation.

}	StubAirPlayModeState;

//===========================================================================================================================
//	CarPlayAppStubDelegate
//===========================================================================================================================

/**
 * 请求向accessory发送数据
 *
 */
typedef int (*RequestAccessoryWrite)(ipod_t /*handle*/,uint8 * /*data*/, uint16 /*len*/,int */*length*/);

/**
 * 请求向accessory读取数据
 *
 */
typedef int (*RequestAccessoryRead)(ipod_t /*handle*/,uint8 * /*data*/, uint16 /*len*/,int */*length*/);

/**
 * 请求screenrecord开始录屏
 *
 */
typedef int (*RequestScreenRecordStart)(ipod_t /*handle*/,int  /*width*/, int /*height*/,int /*fps*/);

/**
 * 请求audio loopback record 开始
 *
 */
typedef int (*RequestAudioRecordStart)(ipod_t /*handle*/,int  /*type*/);


/**
 * 请求audio loopback record 停止
 *
 */
typedef int (*RequestAudioRecordStop)(ipod_t /*handle*/,int  /*type*/);

/**
 * 请求screenrecord停止录屏
 *
 */
typedef int (*RequestScreenRecordStop)(ipod_t /*handle*/);

/**
 * CarPlay会话开始了
 *
 */
typedef void (*AirPlaySessionStarted)(ipod_t /*handle*/);

/**
 * CarPlay会话停止了
 *
 */
typedef void (*AirPlaySessionStoped)(ipod_t /*handle*/);


/**
 * CarPlayTouchEvent
 *
 */
typedef void (*AirPlayTouchEvent)(ipod_t /*handle*/,int /*action*/,int /*x*/,int /*y*/);

/**
 * CarPlayMultiTouchEvent
 *
 */
typedef void (*AirPlayMultiTouchEvent)(ipod_t /*handle*/,int /*action*/,int /*x*/,int /*y*/,int /*action*/,int /*x1*/,int /*y1*/);


/**
 * CarPlayKnobEvent
 *
 */
typedef void (*AirPlayKnobEvent)(ipod_t /*handle*/,int /*button*/,int /*home*/,int /*back*/,int /*x*/,int /*y*/,int /*wheel*/);


/**
 * AirPlayMediaButtonEvent
 *
 */
typedef void (*AirPlayMediaButtonEvent)(ipod_t /*handle*/,int /*mediaButton*/,int /*action*/);

/**
 * ScreenDisplay info
 */
typedef void (*AirPlayScreenDisplay)(ipod_t /*handle*/,int /*width*/,int /*height*/,int /*fps*/,int /*safeWidth*/,int /*safeHeight*/,int /*originXPixels*/,int /*originYPixels*/,int /*fingers*/);

/**
 * AudioFormat info
 */
typedef void (*AirPlayAudioFormatInfo)(ipod_t /*handle*/,int /*streamType*/,int /*sampleRate*/,int /*bits*/,int /*channels*/);


typedef void (*AirPlayModesChanged)(ipod_t /*handle*/,int /*screen*/,int /*audio*/,int /*permAudio*/
		,int /*speechEntity*/,int /*speechMode*/,int /*phoneCall*/,int /*turnByTurn*/);

typedef void (*AirPlayAudioFocusChanged)(ipod_t /*handle*/,int /*audioType*/,int /*ownFocus*/);

typedef void (*AirPlayRequestOpenNetworkCard)(ipod_t /*handle*/,int /*open*/);

typedef void (*AirPlayInfoChanged)(ipod_t /*handle*/,int /*rightHandDrive*/,int /*oemIconVisible*/,int /*nightMode*/);

typedef void (*AirPlaySetNightModeChanged)(ipod_t /*handle*/,int /*nightMode*/);

typedef void (*AirPlayLocationInfoChanged)(ipod_t /*handle*/,char * /*data*/);

typedef void (*AirPlayWifiConfigurationInformation)(ipod_t /*handle*/,const void * /*data*/);

typedef void (*RequestLibusbOperate)(ipod_t /*handle*/,int /*operate*/);

/**
 * 请求Microphone开始
 *
 */
typedef int (*RequestMicrophoneStart)(ipod_t /*handle*/,int  /*type*/);


/**
 * 请求Microphone结束
 *
 */
typedef int (*RequestMicrophoneStop)(ipod_t /*handle*/,int  /*type*/);

/**
 * 请求向accessory的usb vid pid
 *
 */
typedef int (*RequestAccessoryUsbVidPid)(ipod_t /*handle*/,uint16 * /*vid*/, uint16 */*pid*/,int */*length*/);

/**
 * 请求禁用蓝牙
 *
 */
typedef int (*RequestDisableBluetooth)(ipod_t /*handle*/);


/**
 * 原车ViewArea改变
 *
 */
typedef void (*AirPlayViewAreaChanged)(ipod_t /*handle*/);


typedef struct
{
    RequestAccessoryWrite               requestAccessoryWrite_f;
    RequestAccessoryRead                requestAccessoryRead_f;
    RequestScreenRecordStart            requestScreenRecordStart_f;
    RequestScreenRecordStop             requestScreenRecordStop_f;
    RequestAudioRecordStart             requestAudioRecordStart_f;
    RequestAudioRecordStop              requestAudioRecordStop_f;
    AirPlaySessionStarted               airPlaySessionStarted_f;
    AirPlaySessionStoped                airPlaySessionStoped_f;
    AirPlayTouchEvent                	airPlayScreenTouchEvent_f;
    AirPlayMultiTouchEvent              airPlayScreenMultiTouchEvent_f;
    AirPlayKnobEvent                	airPlayScreenKnobEvent_f;
    AirPlayMediaButtonEvent             airPlayScreenMediaButtonEvent_f;
    AirPlayScreenDisplay                airPlayScreenDisplay_f;
    AirPlayAudioFormatInfo              airPlayAudioFormatInfo_f;
    AirPlayModesChanged                 airPlayModesChanged_f;
    AirPlayAudioFocusChanged            airPlayAudioFocusChanged_f;
    AirPlayRequestOpenNetworkCard       airPlayRequestNetworkCard_f;
    AirPlayInfoChanged                  airPlayInfoChanged_f;
    AirPlaySetNightModeChanged          airPlaySetNightMode_f;
    AirPlayLocationInfoChanged          airPlayLocationInfoChanged_f;
    AirPlayWifiConfigurationInformation airPlayWifiConfigurationInformation_f;
    RequestLibusbOperate                requestLibusbOperate_f;
    RequestMicrophoneStart              requestMicrophoneStart_f;
    RequestMicrophoneStop               requestMicrophoneStop_f;
    RequestAccessoryUsbVidPid           requestAccessoryUsbVidPid_f;
    RequestDisableBluetooth				requestDisableBluetooth_f;
    AirPlayViewAreaChanged				airPlayViewAreaChanged_f;

} CarPlayAppStubDelegate;

#define CarPlayAppStubDelegateInit( PTR )	memset( (PTR), 0, sizeof( CarPlayAppStubDelegate ) );

void CarPlayAppStubSetDelegate( const CarPlayAppStubDelegate *inDelegate );
int CarPlayAppStubSendVideo(void* data,int length);
int CarPlayAppStubSendAudio(void* data,int length,int frames);
int CarPlayAppStubReadAudio(void* data,int length,int frames,uint64_t timestamp);
int CarPlayAppStubRequestAudioFocus(int audioChannel);
int CarPlayAppStubAbandonAudioFocus(int audioChannel);

int CarPlayAppStubBack2Home(); //返回原车

int	StartAppleCarPlay( int argc, char **argv );
int	DestroyAppleCarPlay();

int HidGadgetInit();
int HidGadgetUninit();

int UsbGadgetInit();
int UsbGadgetUninit();

int BluetoothGadgetInit();
int BluetoothGadgetUninit();

struct MediaInfo {
	char   title[1024];
	char   artist[1024];
	char   album[1024];
	long   duration;
	long   elapsedTime;
	int    playbackStatus; //0 pause,1 playing
};
int sendMediaLibraryInfo(struct MediaInfo *mediaInfo);

int requestStartLocationInformation(int start);

int requestSendDeviceTimeUpdate( );

struct CallStateInfo {
	char   remoteId[1024];
	char   displayName[1024];
	int    status; //0 Disconnected,3 Connecting,4 Active,6 Disconnecting
	int    direction; //0 Unknown,1 Incoming,2 Outgoing
};
int sendCallStateUpdateInfo(struct CallStateInfo *callStateInfo);

int CarPlayAppStubAudioStart(int audioType,int delay,int sampleRate,int bits,int channels);

int CarPlayAppStubAudioStop(int audioType,int delay,int sampleRate,int bits,int channels);

int requestChangeViewAreaIndex( );

#ifdef  __cplusplus
}  
#endif
#endif //APPLE_APP_STUB_H
